//
//  HDAreaShowView.m
//  WujieLive3.0_beta
//
//  Created by haidong on 2017/12/20.
//  Copyright © 2017年 loghdst. All rights reserved.
//

#import "HDAreaShowView.h"

#define kNormalColor   ColorFromRGB(0xAAAAAA)
#define kSelectedColor ColorFromRGB(0x333333)
#define space 10
#define lineHeight 2
#define buttonHeight 30.f

@interface HDAreaShowView ()

@property (strong, nonatomic)UIView *slideLineView;
@property (strong, nonatomic)UIButton *oneLevelBtn;
@property (strong, nonatomic)UIButton *twoLevelBtn;
@property (strong, nonatomic)UIButton *threeLevelBtn;
@property (strong, nonatomic)UIButton *sureBtn;

@end

@implementation HDAreaShowView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfig];

    }
    return self;
}

- (void)initConfig {
    self.backgroundColor = ColorFromRGB(0xF5F5F5);

    [self addSubview:self.oneLevelBtn];
    [self addSubview:self.twoLevelBtn];
    [self addSubview:self.threeLevelBtn];
    [self addSubview:self.sureBtn];
    
    [self.oneLevelBtn setTitle:@"请选择" forState:UIControlStateNormal];
    WS(weakSelf)
    [self.oneLevelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(space);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.height.mas_equalTo(buttonHeight);
    }];
    [self.twoLevelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.oneLevelBtn.mas_right).offset(space);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.height.mas_equalTo(buttonHeight);
    }];
    [self.threeLevelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.twoLevelBtn.mas_right).offset(space);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.height.mas_equalTo(buttonHeight);
    }];
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-space);
        make.centerY.mas_equalTo(weakSelf.mas_centerY);
        make.height.mas_equalTo(buttonHeight);
    }];
    
    [self addSubview:self.slideLineView];
    [self.slideLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(weakSelf.mas_bottom);
        make.centerX.mas_equalTo(weakSelf.oneLevelBtn.mas_centerX);
        make.width.mas_equalTo(weakSelf.oneLevelBtn.mas_width);
        make.height.mas_equalTo(2);
    }];
    
    [self.oneLevelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.twoLevelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.threeLevelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.sureBtn addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)showMessage:(NSString *)message atIndex:(NSInteger)index hasMore:(BOOL)hasMore {
    switch (index) {
        case 0:
            [self.oneLevelBtn setTitle:message forState:UIControlStateNormal];
            self.oneLevelBtn.selected = YES;
            
            [self.twoLevelBtn setTitle:@"" forState:UIControlStateNormal];
            self.twoLevelBtn.selected = NO;
            [self.threeLevelBtn setTitle:@"" forState:UIControlStateNormal];
            self.threeLevelBtn.selected = NO;
            
            if (hasMore) {
                [self.twoLevelBtn setTitle:@"请选择" forState:UIControlStateNormal];
                [self slideViewMovetoButton:self.twoLevelBtn];
            }
            break;
        case 1:
            [self.twoLevelBtn setTitle:message forState:UIControlStateNormal];
            self.twoLevelBtn.selected = YES;
            
            [self.threeLevelBtn setTitle:@"" forState:UIControlStateNormal];
            self.threeLevelBtn.selected = NO;
            
            if (hasMore) {
                [self.threeLevelBtn setTitle:@"请选择" forState:UIControlStateNormal];
                [self slideViewMovetoButton:self.threeLevelBtn];
            }
            break;
        case 2:
            [self.threeLevelBtn setTitle:message forState:UIControlStateNormal];
            self.threeLevelBtn.selected = YES;
            break;
        default:
            break;
    }
}

- (void)slideViewMovetoButton:(UIButton *)button {
    WS(weakSelf)
    [self setNeedsUpdateConstraints];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.slideLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(weakSelf.mas_bottom);
            make.centerX.mas_equalTo(button.mas_centerX);
            make.width.mas_equalTo(button.mas_width);
            make.height.mas_equalTo(2);
        }];
        [UIView animateWithDuration:0.2 animations:^{
            [self layoutIfNeeded];
        }];
    });
}

- (void)buttonAction:(UIButton *)sender {
    // 未选中时不可点击
    if (!sender.selected) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(viewActionWithView:tag:data:)]) {
        [self.delegate viewActionWithView:self tag:sender.tag - 20 data:nil];
    }
    [self slideViewMovetoButton:sender];
}

- (void)sureButtonAction:(UIButton *)sender {
    if ([self.oneLevelBtn.titleLabel.text isEqualToString:@"请选择"] ||
        [self.twoLevelBtn.titleLabel.text isEqualToString:@"请选择"] ||
        [self.threeLevelBtn.titleLabel.text isEqualToString:@"请选择"]) {
        return;
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(viewActionWithView:tag:data:)]) {
        [self.delegate viewActionWithView:self tag:sender.tag - 20 data:nil];
    }
}

- (UIView *)slideLineView {
    if (!_slideLineView) {
        _slideLineView = [[UIView alloc] init];
        _slideLineView.backgroundColor = ColorFromRGB(0xF03D37);
    }
    return _slideLineView;
}

- (UIButton *)oneLevelBtn {
    if (!_oneLevelBtn) {
        _oneLevelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _oneLevelBtn.adjustsImageWhenHighlighted = NO;
        [_oneLevelBtn setTitleColor:kNormalColor forState:UIControlStateNormal];
        [_oneLevelBtn setTitleColor:kSelectedColor forState:UIControlStateSelected];
        _oneLevelBtn.titleLabel.font = FontSize(14.f);
        _oneLevelBtn.tag = 20;
    }
    return _oneLevelBtn;
}

- (UIButton *)twoLevelBtn {
    if (!_twoLevelBtn) {
        _twoLevelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _twoLevelBtn.adjustsImageWhenHighlighted = NO;
        [_twoLevelBtn setTitleColor:kNormalColor forState:UIControlStateNormal];
        [_twoLevelBtn setTitleColor:kSelectedColor forState:UIControlStateSelected];
        _twoLevelBtn.titleLabel.font = FontSize(14.f);
        _twoLevelBtn.tag = 21;
    }
    return _twoLevelBtn;
}

- (UIButton *)threeLevelBtn {
    if (!_threeLevelBtn) {
        _threeLevelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _threeLevelBtn.adjustsImageWhenHighlighted = NO;
        [_threeLevelBtn setTitleColor:kNormalColor forState:UIControlStateNormal];
        [_threeLevelBtn setTitleColor:kSelectedColor forState:UIControlStateSelected];
        _threeLevelBtn.titleLabel.font = FontSize(14.f);
        _threeLevelBtn.tag = 22;
    }
    return _threeLevelBtn;
}

- (UIButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _sureBtn.adjustsImageWhenHighlighted = NO;
        [_sureBtn setTitleColor:kSelectedColor forState:UIControlStateNormal];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        _sureBtn.titleLabel.font = FontSize(14.f);
        _sureBtn.tag = 23;
    }
    return _sureBtn;
}

@end
